/**
 * 添加用户弹出框 （查看，修改，添加）
 */
import React from 'react';
import { connect } from 'umi';
import { Modal, message } from 'antd';
import BModal from '@complexComp/BModal';
import Cont from './Cont';

@connect(({ UserList }) => ({
  UserList,
}))
export default class extends React.Component {
  forms = {};
  handleOk = async () => {
    const { addVisibleModalType } = this.props.UserList;
    if (addVisibleModalType == 'add') this.add();
    if (addVisibleModalType == 'update') this.update();
  };

  // 添加用户
  add = async () => {
    const { userForm } = this.forms;
    var attr = [userForm?.validateFields()];
    var [userFormData] = await Promise.all(attr);

    const { roleVoList } = userFormData;
    var reqData = {
      ...userFormData,
      roleVoList: roleVoList.map(item => ({ id: item })),
    };

    var rs = await this.props.dispatch({
      type: 'UserList/addUserList',
      payload: reqData,
    });

    if (!rs.success) {
      var errText = '操作失败';
      if (rs.subCode == '100103') errText = '重复的用户名';
      if (rs.subCode == '100109') errText = '重复的邮箱';
      if (rs.subCode == '100107') errText = '重复的手机号';
      if (rs.subCode == '100108') errText = '没有此手机号';
      if (rs.subCode == '100203') errText = '重复的角色名';
      message.error(errText);
      return;
    }

    // 成功
    if (rs.success) {
      message.success('添加成功');
      this.onClose();
      this.forms.userForm.resetFields();
    }
    //  更新列表
    this.props.parentThis.getList();
  };

  // 修改用户
  update = async () => {
    const { addVisibleModalUserId, password } = this.props.UserList;
    const { userForm } = this.forms;
    var attr = [userForm?.validateFields()];
    var [userFormData] = await Promise.all(attr);
    const { roleVoList } = userFormData;
    console.log(userFormData);
    var reqData = {
      email:userFormData.email,
      name:userFormData.name,
      telNum:userFormData.telNum,
      username:userFormData.username,
      id: addVisibleModalUserId,
      roleVoList: roleVoList.map(item => ({ id: item })),
    };
    if (password) {
      reqData.password = password;
    }

    var rs = await this.props.dispatch({
      type: 'UserList/updateUserInfoById',
      payload: reqData,
    });

    if (!rs.success) {
      var errText = '操作失败';
      if (rs.subCode == '100103') errText = '重复的用户名';
      if (rs.subCode == '100109') errText = '重复的邮箱';
      if (rs.subCode == '100107') errText = '重复的手机号';
      if (rs.subCode == '100108') errText = '没有此手机号';
      if (rs.subCode == '100203') errText = '重复的角色名';
      message.error(errText);
      return;
    }
    // 成功
    if (rs.success) {
      message.success('修改成功');
      this.onClose();
      //  更新列表
      this.props.parentThis.getList();
      this.forms.userForm.resetFields();
    }
  };

  //获取弹窗type类型
  getTitle = () => {
    const { addVisibleModalType } = this.props.UserList;
    if (addVisibleModalType == 'add') return '创建用户';
    if (addVisibleModalType == 'update') return '修改用户';
    if (addVisibleModalType == 'view') return '查看用户';
  };

  //关闭弹窗
  onClose = () => {
    this.props.dispatch({
      type: 'UserList/setState',
      payload: { addVisible: false },
    });
  };

  render() {
    const { addVisible, addVisibleModalType } = this.props.UserList;
    var config = {};
    if (addVisibleModalType == 'view') config.footer = null;
    return (
      <BModal
        title={this.getTitle()}
        width={'500px'}
        onOk={this.handleOk}
        close={this.onClose}
        visible={addVisible}
        moreConfig={config}
      >
        <Cont init={form => (this.forms.userForm = form)} />
      </BModal>
    );
  }
}
